<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "mkt_source_promotion".
 *
 * @property string $id
 * @property string $user_id
 * @property string $promotion_channel_id
 * @property string $promotion_fan_industry
 * @property string $area_id
 * @property string $promotion_fan_count
 */
class MktSourcePromotion extends \yii\db\ActiveRecord
{
    public $parent_promotion_fan_industry;
    public $parent_area_id;

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'mkt_source_promotion';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['user_id', 'promotion_channel_id', 'promotion_fan_industry', 'area_id', 'promotion_fan_count'], 'integer'],
            [['parent_promotion_fan_industry','parent_area_id'],'safe']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'user_id' => '用户编号',
            'promotion_channel_id' => '推广渠道',                   // 微信自媒体，论坛，网站...
            'promotion_fan_industry' => '粉丝行业',         // 多选?
            'area_id' => '粉丝区域',
            'promotion_fan_count' => '粉丝数量',
        ];
    }

    /**
     * @inheritdoc
     * @return MktSourcePromotionQuery the active query used by this AR class.
     */
    public static function find()
    {
        return new MktSourcePromotionQuery(get_called_class());
    }

    public function getUser()
    {
        return $this->hasOne(MktUser::className(), ['id' => 'user_id']);
    }
    
    public function getIndustry()
    {
        return $this->hasOne(MktIndustry::className(), ['id' => 'promotion_fan_industry']);
    }

    public function getParentIndustry()
    {
        $industry = MktIndustry::findOne(['id' => $this->promotion_fan_industry]);
        $parent_industry = MktIndustry::findOne(['id' => $industry->parent_id]);
        return $parent_industry;
    }

    public function getArea()
    {
        return $this->hasOne(MktArea::className(), ['id' => 'area_id']);
    }

    public function getIndustryPaths()
    {
        return empty($this->industry) ? '' : $this->industry->getPaths();
    }

    public function getAreaPaths()
    {
        return empty($this->area) ? '' : $this->area->getPaths();
    }

    static function getChannelOption($key=null)
    {
        $arr = array(
            0 => '微信自媒体',
            1 => '论坛',
            2 => '网站',
            7 => '其它',
        );
        return $key === null ? $arr : (isset($arr[$key]) ? $arr[$key] : '');
    }

    static function getFanIndustryOption($key=null)
    {
        $arr = array(
            0 => '婴童',
            1 => '小学生',
        );
        return $key === null ? $arr : (isset($arr[$key]) ? $arr[$key] : '');
    }

}
